import request from '@/utils/request'

/**
 * 票种管理API
 * 基础URL: http://localhost:8089
 * 
 * API文档路径:
 * 获取票种列表: /api/events/{eventId}/tickets (GET)
 * 添加票种: /api/tickets (POST)  
 * 修改票种: /api/tickets/{id} (PUT)
 * 删除票种: /api/tickets/{id} (DELETE)
 * 获取票种销售统计: /api/tickets/{id}/stats (GET)
 */

/**
 * 获取某活动的票种配置列表
 * @param {Number|String} eventId - 活动ID，如果是'all'则获取所有票种
 * @returns {Promise}
 */
export function getTicketList(eventId) {
  // 如果是获取所有票种
  if (eventId === 'all') {
    return request({
      url: '/api/tickets',
      method: 'get'
    })
  }
  // 获取指定活动的票种
  return request({
    url: `/api/events/${eventId}/tickets`,
    method: 'get'
  })
}

/**
 * 添加一个新票种
 * @param {Object} data - 票种数据，包含eventId, name, price, totalStock, limitPerUser, description等
 * @returns {Promise} 
 * 
 * 请求参数示例:
 * {
 *   "eventId": 1,
 *   "name": "成人票",
 *   "price": 99.00,
 *   "totalStock": 100,
 *   "limitPerUser": 2,
 *   "description": "成人票说明"
 * }
 * 
 * 返回数据示例:
 * {
 *   "code": 200,
 *   "msg": "创建成功",
 *   "data": {
 *     "id": 1,
 *     "eventId": 1,
 *     "name": "成人票",
 *     "price": 99.00,
 *     "totalStock": 100,
 *     "soldCount": 0,
 *     "limitPerUser": 2,
 *     "description": "成人票说明",
 *     "createdAt": "2023-05-15 10:30:00",
 *     "updatedAt": "2023-05-15 10:30:00"
 *   }
 * }
 */
export function createTicket(data) {
  return request({
    url: '/api/tickets',
    method: 'post',
    data
  })
}

/**
 * 修改票种信息
 * @param {Number} eventId - 活动ID
 * @param {Number} ticketId - 票种ID
 * @param {Object} data - 票种数据
 * @returns {Promise}
 */
export function updateTicket(eventId, ticketId, data) {
  return request({
    url: `/api/events/${eventId}/tickets/${ticketId}`,
    method: 'put',
    data
  })
}

/**
 * 删除票种（逻辑删除）
 * @param {Number} id - 票种ID
 * @returns {Promise}
 */
export function deleteTicket(id) {
  return request({
    url: `/api/tickets/${id}`,
    method: 'delete'
  })
}

/**
 * 查询某票种销售数据
 * @param {Number} id - 票种ID
 * @returns {Promise}
 */
export function getTicketStats(id) {
  return request({
    url: `/api/tickets/${id}/stats`,
    method: 'get'
  })
} 